package cn.zhejianglao.shirotemplate.service.role.service;

import cn.zhejianglao.shirotemplate.common.model.*;
import cn.zhejianglao.shirotemplate.exception.BusinessException;
import cn.zhejianglao.shirotemplate.exception.ExceptionCode;
import cn.zhejianglao.shirotemplate.service.role.dao.IRoleMapper;
import cn.zhejianglao.shirotemplate.service.user.dao.IUserMapper;
import cn.zhejianglao.shirotemplate.service.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService implements IRoleService {

    @Autowired
    private IRoleMapper roleMapper;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserMapper userMapper;

    @Override
    public void create(RoleVO roleVO) {
        if (roleMapper.selectCountByRoleName(roleVO.getRoleName()) > 0) {
            throw new BusinessException(
                    ExceptionCode.Permission.ROLE_ALREADY_EXISTS_CODE,
                    ExceptionCode.Permission.ROLE_ALREADY_EXISTS_MSG
            );
        }
        roleMapper.insertRole(roleVO);
    }

    @Override
    public void deleteRole(Long roleId) {
        roleMapper.deleteRoleById(roleId);
    }

    @Override
    public PagedResult<RoleVO> listRoles(int size, int page) {
        PageRequest request = new PageRequest(size, page);
        List<RoleVO> result = roleMapper.selectByPage(request);
        return new PagedResult<>(result, request);
    }

    @Override
    public void updateRole(RoleVO roleVO) {
        if (roleVO.getId() == null) {
            throw new BusinessException(
                    ExceptionCode.Param.ILLEGAL_PARAM_CODE,
                    "角色id为空"
            );
        }
        Long currentUserID = userService.getCurrentUser().getId();
        roleVO.setModifiedBy(currentUserID);
        roleMapper.updateRole(roleVO);
    }

    @Override
    public void authorize(UserRoleVO userRoleRelation) {
        validationRelation(userRoleRelation);
        if (roleMapper.selectCountByRelation(userRoleRelation) > 0) {
            throw new BusinessException(
                    ExceptionCode.Common.RESOURCE_ALREADY_EXISTS_CODE,
                    ExceptionCode.Common.RESOURCE_ALREADY_EXISTS_MSG
            );
        }
        Long currentUserId = userService.getCurrentUser().getId();
        userRoleRelation.setCreatedBy(currentUserId);
        roleMapper.insertUserRoleRelation(userRoleRelation);
    }

    @Override
    public void cancelAuthorization(UserRoleVO relation) {
        validationRelation(relation);
        if (roleMapper.selectCountByRelation(relation) == 0) {
            throw new BusinessException(
                    ExceptionCode.Common.RESOURCE_NOT_EXISTS_CODE,
                    ExceptionCode.Common.RESOURCE_NOT_EXISTS_MSG
            );
        }
        roleMapper.deleteUserRoleRelation(relation);

    }

    /**
     * 校验关系，用户id，角色id在库中都需要存在
     *
     * @param relation 关系
     */
    private void validationRelation(UserRoleVO relation) {
        if (userMapper.selectCountById(relation.getUserId()) == 0) {
            throw new BusinessException(
                    ExceptionCode.Common.RESOURCE_NOT_EXISTS_CODE,
                    "用户不存在"
            );
        }
        if (roleMapper.selectCountById(relation.getRoleId()) == 0) {
            throw new BusinessException(
                    ExceptionCode.Common.RESOURCE_NOT_EXISTS_CODE,
                    "角色不存在"
            );
        }
    }
}
